package com.nuoyuan.wx.wx.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoyuan.wx.admin.controller.ProductController;
import com.nuoyuan.wx.aop.NoLoginRequired;
import com.nuoyuan.wx.common.controller.BaseController;
import com.nuoyuan.wx.common.utils.DateUtil;
import com.nuoyuan.wx.common.utils.GsonUtil;
import com.nuoyuan.wx.dao.mapper.NoticeMapper;
import com.nuoyuan.wx.dao.po.NoticePo;
import com.nuoyuan.wx.vo.NoticeVo;
import com.nuoyuan.wx.vo.PageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 公告
 * @Package com.nuoyuan.wx.wx.controller
 * @ClassName NoticeController
 * @Description TODO
 * @Author linus
 * @Date 2020-02-21 16:01
 * @Version 1.0
 **/
@RestController
public class NoticeController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(NoticeController.class);
    @Autowired
    private NoticeMapper noticeMapper;

    @NoLoginRequired
    @RequestMapping(value = "/wx/notices", method = RequestMethod.GET)
    @ResponseBody
    public String notices(PageVo page, HttpServletRequest request, HttpServletResponse response) {
        try {
            Wrapper<NoticePo> queryWrapper = new QueryWrapper<>();
            ((QueryWrapper<NoticePo>) queryWrapper).eq("is_deleted", 0).orderByDesc("order_date");
            IPage p = new Page();
            p.setCurrent(page.getPageNo());
            p.setSize(page.getPageSize());
            IPage<NoticePo> pages = noticeMapper.selectPage(p, queryWrapper);
            if(pages == null || CollectionUtils.isEmpty(pages.getRecords())) {
                PageVo emptyP = new PageVo<>();
                emptyP.setItems(new ArrayList());
                return GsonUtil.toJson(emptyP);
            }

            List<NoticePo> pos = pages.getRecords();
            List<NoticeVo> vos = new ArrayList<>();
            for(NoticePo po : pos) {
                NoticeVo vo = new NoticeVo();
                BeanUtils.copyProperties(po, vo);
                vo.setCreateTime(DateUtil.formatDate(po.getCreateTime(), DateUtil.FMT_DATE_YYYYMMDD_HHMMSS));
                vos.add(vo);
            }
            page.setItems(vos);
            page.setCount(pages.getTotal());
            return GsonUtil.toJson(page);
        } catch (Exception e) {
            log.error(e.getMessage());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
        }
    }

    @NoLoginRequired
    @RequestMapping(value = "/wx/banners", method = RequestMethod.GET)
    @ResponseBody
    public String banners(PageVo page, HttpServletRequest request, HttpServletResponse response) {
//        page.setItems(page.getRecords());
//        page.setCount(page.getTotal());
        return GsonUtil.toJson(page);
    }


    /**
     * 详情
     *
     * @param param
     * @param request
     * @param response
     * @return
     */
    @NoLoginRequired
    @RequestMapping(value = "/wx/notice", method = RequestMethod.GET)
    @ResponseBody
    public String detail(NoticePo param, HttpServletRequest request, HttpServletResponse response) {
        try {
            Wrapper<NoticePo> queryWrapper = new QueryWrapper<>();
            ((QueryWrapper<NoticePo>) queryWrapper).eq("notice_id", param.getNoticeId());
            NoticePo po = noticeMapper.selectOne(queryWrapper);

            NoticeVo vo = new NoticeVo();
            BeanUtils.copyProperties(po, vo);
            vo.setCreateTime(DateUtil.formatDate(po.getCreateTime(), DateUtil.FMT_DATE_YYYYMMDD_HHMMSS));

            return GsonUtil.toJson(vo);
        } catch (Exception e) {
            log.error(e.getMessage());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
        }
    }
}
